<?php
/**
 * Author:   tangxiaowen
 * Email:    846506584@qq.com
 * Date:     2019/4/4 0004
 * Describe: 文件上传
 */

namespace app\admin\controller;

class Upload extends Common {

    /**
     * 文件上传
     */
    public function upload()
    {
        //获取图片资源
        $file = request()->file('file');
        if(empty($file)) {
            return json(['code' => 203,'msg' => '请选择上传文件','data' => []]);
        }

        //获取文件配置校验参数
        $checkParam = config('queue.upload_file');

        //校验文件并移动到指定目录
        $info = $file->validate(['size'=>$checkParam['size'],'ext'=>$checkParam['ext']])->move('./uploads');
        if($info){
            $imgUrl = 'uploads/' . $info->getSaveName();
            $imgUrl = str_replace('\\','/',$imgUrl);
            return json(['code' => 200,'msg' => '上传成功','data' => ['pic' => $imgUrl]]);
        }else{
            return json(['code' => 205,'msg' => $file->getError(),'data' => []]);
        }
    }

}